package com.ruoyi.system.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PageUtil {

    public static Page page(List<Object> list, Integer pageSize, Integer pageNum) {
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=10;
        }
        //开启分页
        Integer count = list.size(); //记录总数
        Integer pageCount = 0; //页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; //开始索引
        int toIndex = 0; //结束索引

        if (pageNum > pageCount) {
            pageNum = pageCount;
        }
        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setCount(count);
        page.setPageCount(pageCount);
        page.setToIndex(toIndex);
        page.setFromIndex(fromIndex);
        return page;
    }


    public static Map<String, Object> success(Page page,List pageList)
    {
        Map<String, Object> map=new HashMap<>();
        map.put("total", page.getCount());
        map.put("maxPage", page.getPageCount());
        map.put("pageSize", page.getPageSize());
        map.put("pageNum", page.getPageNum());
        map.put("msg", "成功");
        map.put("code", 200);
        map.put("data", pageList);
        return map;
    }
    public static Map<String, Object> error()
    {
        Map<String, Object> map=new HashMap<>();
        map.put("msg", "成功");
        map.put("code", 200);
        map.put("data", null);
        return map;
    }

}
